package com.autoglm.data.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import java.util.Date

/**
 * 任务数据模型
 */
@Parcelize
data class Task(
    val id: String,
    val description: String,
    val userInput: String,
    val steps: List<ActionStep>,
    val status: TaskStatus,
    val createdAt: Date,
    val updatedAt: Date,
    val completedAt: Date? = null,
    val errorMessage: String? = null,
    val parameters: Map<String, String> = emptyMap(),
    val applicationFlow: List<String> = emptyList()
) : Parcelable

/**
 * 操作步骤数据模型
 */
@Parcelize
data class ActionStep(
    val id: String,
    val taskId: String,
    val stepNumber: Int,
    val description: String,
    val actionType: ActionType,
    val targetApp: String? = null,
    val targetElement: UIElement? = null,
    val inputText: String? = null,
    val coordinates: Point? = null,
    val status: StepStatus,
    val retryCount: Int = 0,
    val errorMessage: String? = null,
    val executedAt: Date? = null
) : Parcelable

/**
 * UI元素数据模型
 */
@Parcelize
data class UIElement(
    val id: String? = null,
    val className: String? = null,
    val contentDescription: String? = null,
    val text: String? = null,
    val bounds: Rect? = null,
    val isClickable: Boolean = false,
    val isEnabled: Boolean = true
) : Parcelable

/**
 * 矩形区域数据模型
 */
@Parcelize
data class Rect(
    val left: Int,
    val top: Int,
    val right: Int,
    val bottom: Int
) : Parcelable {
    val centerX: Int get() = (left + right) / 2
    val centerY: Int get() = (top + bottom) / 2
    val width: Int get() = right - left
    val height: Int get() = bottom - top
}

/**
 * 点坐标数据模型
 */
@Parcelize
data class Point(
    val x: Int,
    val y: Int
) : Parcelable

/**
 * 任务状态枚举
 */
enum class TaskStatus {
    PENDING,        // 待执行
    IN_PROGRESS,    // 执行中
    COMPLETED,      // 已完成
    FAILED,         // 执行失败
    CANCELLED       // 已取消
}

/**
 * 步骤状态枚举
 */
enum class StepStatus {
    PENDING,        // 待执行
    IN_PROGRESS,    // 执行中
    COMPLETED,      // 已完成
    FAILED,         // 执行失败
    SKIPPED         // 已跳过
}

/**
 * 操作类型枚举
 */
enum class ActionType {
    LAUNCH_APP,         // 启动应用
    CLICK,              // 点击
    LONG_CLICK,         // 长按
    SWIPE,              // 滑动
    INPUT_TEXT,         // 输入文本
    SCREENSHOT,         // 截图
    WAIT,               // 等待
    BACK_PRESS,         // 返回
    HOME_PRESS,         // 主页
    RECENT_APPS,        // 最近应用
    SCROLL,             // 滚动
    ZOOM,               // 缩放
    ROTATE              // 旋转
}

/**
 * 任务执行结果
 */
data class TaskResult(
    val taskId: String,
    val success: Boolean,
    val message: String,
    val outputFiles: List<String> = emptyList(),
    val executionTime: Long,
    val errorDetails: String? = null
)

/**
 * 步骤执行结果
 */
data class StepResult(
    val stepId: String,
    val success: Boolean,
    val message: String,
    val screenshotPath: String? = null,
    val executionTime: Long,
    val errorDetails: String? = null
)
